
let btn = document.getElementById('register-btn');
let inputAvatar = document.getElementById("avatar");
let inputNick = document.getElementById('inputNick');
let inputID = document.getElementById('inputID');
let inputPassword1 = document.getElementById('inputPassword1');
let inputPassword2 = document.getElementById('inputPassword2');

let nick_msg = document.getElementById("nick-msg");
let id_msg = document.getElementById("id-msg");
let password1_msg = document.getElementById("password1-msg");
let password2_msg = document.getElementById("password2-msg");

let avatar;
let nick;
let id;
let password1;
let password2;
let gender;

let nick_class = inputNick.getAttribute("class");
let id_class = inputID.getAttribute("class");
let password1_class = inputPassword1.getAttribute("class");
let password2_class = inputPassword2.getAttribute("class");

let nick_invalid_class = nick_class.concat(" is-invalid");
let id_invalid_class = id_class.concat(" is-invalid");
let password1_invalid_class = password1_class.concat(" is-invalid");
let password2_invalid_class = password2_class.concat(" is-invalid");

let nick_valid_class = nick_class.concat(" is-valid");
let id_valid_class = id_class.concat(" is-valid");
let password1_valid_class = password1_class.concat(" is-valid");
let password2_valid_class = password2_class.concat(" is-valid");

//头像上传
function assignment() {
  inputAvatar.src = window.URL.createObjectURL(document.getElementById('avatarUpload').files[0]);
}

function checkForm() {
  nick = inputNick.value;
  id = inputID.value;
  password1 = inputPassword1.value;
  password2 = inputPassword2.value;
  gender = $('#inputGender input[name="gender"]:checked ').val();

  if(nick === "") {
    inputNick.setAttribute("class", nick_invalid_class);
    nick_msg.innerHTML = "请设置您的昵称";
  } else  if(id === "") {
    inputNick.setAttribute("class", nick_valid_class);
    inputID.setAttribute("class", id_invalid_class);
    id_msg.innerHTML = "请设置您的账号";
  } else if(password1 === "" || password2 === "") {
    inputNick.setAttribute("class", nick_valid_class);
    inputID.setAttribute("class", id_valid_class);
    inputPassword1.setAttribute("class", password1_invalid_class);
    inputPassword2.setAttribute("class", password2_invalid_class);
    password2_msg.innerHTML = "请设置正确的密码";
  } else if(password1 !== password2) {
    inputNick.setAttribute("class", nick_valid_class);
    inputID.setAttribute("class", id_valid_class);
    inputPassword1.setAttribute("class", password1_invalid_class);
    inputPassword2.setAttribute("class", password2_invalid_class);
    password2_msg.innerHTML = "两次密码不一致";
  } else {
    inputNick.setAttribute("class", nick_valid_class);
    inputID.setAttribute("class", id_valid_class);
    inputPassword1.setAttribute("class", password1_valid_class);
    inputPassword2.setAttribute("class", password2_valid_class);
    submit();
  }
}

function focusInput() {
  inputNick.setAttribute("class", nick_class);
  inputID.setAttribute("class", id_class);
  inputPassword1.setAttribute("class", password1_class);
  inputPassword2.setAttribute("class", password2_class);
}

function submit() {
  let image_url = "https://i.loli.net/2021/03/06/4MwIy63d2Fpg597.png";

  $.ajax({
    type: "POST",
    url: "http://test.eslans.com:8000/user/register",
    data: {
      id: id,
      name: nick,
      password: password1,
      image_url: image_url,
      gender: gender,
      signature: "这个人很懒，什么都没留下。"
    },
    success:
    function(result) {
      if(result.succ) {
        Cookies.set('UID', result.data.uid);
        Cookies.set('nick', result.data.name);
        Cookies.set('image_url', result.data.image_url);
        Cookies.set('gender', result.data.gender);
        Cookies.set('signature', result.data.signature);
        Cookies.set('hidden_Point', result.data.hidden_Point);
        Cookies.set('winNum', result.data.winNumber);
        Cookies.set('loseNum', result.data.loseNumber);
        Cookies.set('drawNum', result.data.drawNumber);
        window.location.href="main.html";
      } else {
        inputID.setAttribute("class", id_invalid_class);
        id_msg.innerHTML = "账号已存在";
      }  
    },
  });
}

btn.onclick = function () {
  checkForm();
}

$("body").keypress(function(event){
  if(event.which === 13) { 
      btn.click();
  }
})